package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.List;

public class LC54 {

    public List<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> res = new ArrayList<>();
        int top = 0, left = 0, right = matrix[0].length - 1, bottom = matrix.length - 1;
        while (right >= left && bottom >= top) {
            // left-->right
            for (int i = left; i < right; i++) {
                res.add(matrix[top][i]);
            }
            // top-->bottom
            for (int i = top; i < bottom; i++) {
                res.add(matrix[i][right]);
            }
            if (left < right && top < bottom) {
                // right-->left
                for (int i = right; i > left; i--) {
                    res.add(matrix[bottom][i]);
                }
                // bottom-->top
                for (int i = bottom; i > top; i--) {
                    res.add(matrix[i][left]);
                }
            }
            left++;
            right--;
            top++;
            bottom--;

        }
        return res;
    }

    public static void main(String[] args) {

    }
}
